// import * as express from 'express';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compression = require('compression');
var config = require('./config.json');
var app = express();
app.set('env', 'production');
app.set('json spaces', 4);
// view engine setup
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'jade');
app.use(favicon(path.join(__dirname, './public/images/favicon.ico')));
app.use(compression());

app.use(bodyParser.json({
    limit: '1mb'
}));
app.use(bodyParser.urlencoded({
    limit: '1mb',
    extended: false
}));
app.use(cookieParser("entaro adun"));
app.use(express.static(path.join(__dirname, './assets')));
app.use(express.static(path.join(__dirname, './public')));
app.use(logger('dev'));

//加载angular的分布视图
require('./utility/autoRenderPartial')(app);
//注册路由
require('./routes/login/index')(app);//先注册登录页面,登陆后,后续的页面才能被展示
require('./routes/metro/index')(app);


app.use('/*', function (req, res) {
    res.redirect('/metro');
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('404 Not Found');
    err.status = 404;
    next(err);
});
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});
app.set('port', config.port || 3000);
var server = app.listen(app.get('port'), function () {
    console.log("网站启动在", server.address().port);
});
